﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace ThuPhap
{
    public partial class ChooseColorPage : PhoneApplicationPage
    {
        public delegate void FinishChooseBrushDelegate(Brush Brush);
        public delegate void FinishChooseColorDelegate(Color Color);
        public static event FinishChooseBrushDelegate FinishChooseBrushEvent;
        public static event FinishChooseColorDelegate FinishChooseColorEvent;
        private string type = string.Empty;
        public ChooseColorPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string color = string.Empty;
            if (NavigationContext.QueryString.TryGetValue("color", out color) && NavigationContext.QueryString.TryGetValue("type", out type))
            {
                string[] array = color.Split(',');
                this.ColorPicker.Color = Color.FromArgb(byte.Parse(array[0]), byte.Parse(array[1]), byte.Parse(array[2]), byte.Parse(array[3]));
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            if (type == "main")
            {
                if (FinishChooseBrushEvent != null)
                {
                    FinishChooseBrushEvent(new SolidColorBrush(this.ColorPicker.Color));
                    NavigationService.GoBack();
                }
            }
            else if (type == "background")
            {
                if (FinishChooseColorEvent != null)
                {
                    FinishChooseColorEvent(this.ColorPicker.Color);
                    //NavigationService.GoBack();
                    NavigationService.RemoveBackEntry();
                    NavigationService.GoBack();
                    ChooseColorPage.FinishChooseColorEvent = null;
                    
                }
            }

            
            
        }


        private void Rectangle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Rectangle border = sender as Rectangle;
            SolidColorBrush brush = (SolidColorBrush)border.Fill;
            this.ColorPicker.Color = brush.Color;
        }
    }
}